package com.yc.meeting_attendance.service;


import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.hwangjr.rxbus.RxBus;
import com.yc.meeting_attendance.MyApp;
import com.yc.nfc.CardInfo;
import com.yc.nfc.Convert;
import com.yc.nfc.NfcClient;

import java.io.IOException;
import java.security.InvalidParameterException;


/**
 * Created by Administrator on 2016-04-13.
 */
public class CheckCardExistService extends Service {
    private static final String LOGTAG = "CheckCardExistService";
    public static NfcClient client;
    private CardSever cardsever;
    private byte mSector;
    private byte[] mKeys;

    @Override
    public void onCreate() {
        Log.d(LOGTAG, "------------onCreate-------------------");
        String ob = "0B"; //扇区
        mSector = (byte)MyApp.getsInstance().sharePreferenceUtil.getCarSection();

//        String NFCKEY = "19-7F-D8-73-67-DC"; //key值
//        String keys = NFCKEY.replace("-", "");
        mKeys = Convert.HexToByteArr(MyApp.getsInstance().sharePreferenceUtil.getCarKey());
        Log.d("TAG","B:"+MyApp.getsInstance().sharePreferenceUtil.getCarKey());
        if (client == null)
            client = new NfcClient(MyApp.getsInstance().sharePreferenceUtil.getCom(), mSector, mKeys);
        try {
            client.open();
        } catch (InvalidParameterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (cardsever == null)
            cardsever = new CardSever();
        cardsever.start();

    }

    @Override
    public void onStart(Intent intent, int startId) {
        Log.d(LOGTAG, "------------onStart-------------------");
        bRun = true;
    }

    @Override
    public void onDestroy() {
        Log.d(LOGTAG, "------------onDestroy-------------------");
        try {
            bRun = false;
            cardsever.interrupt();
            client.close();

        } catch (Exception ex) {

        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onRebind(Intent intent) {
    }

    static boolean bRun = true;

    public static void startAttendance() {

        bRun = true;
    }

    public static void stopAttendance() {
        bRun = false;
    }

    class CardSever extends Thread {
        public CardSever() {
            // TODO Auto-generated constructor stub
        }

        @Override
        public void run() {
            super.run();
            while (true) {
                if (bRun) {
                    if (client.isOpen()) {
                        CardInfo info = client.readMoney();
                        if (info!=null)
                            RxBus.get().post("card", info);
                    }
                    try {
                        Thread.sleep(200);

                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            }
        }
    }


}
